AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

function ENT:Initialize()
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	--self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetCollisionGroup(COLLISION_GROUP_INTERACTIVE_DEBRIS)
	self.Entity:DrawShadow(false)
	local physobj = self.Entity:GetPhysicsObject()
	if not ((physobj != NULL) and (physobj != nil) and (physobj:IsValid())) then self.Entity:Remove() end
	physobj:SetMass(self.Entity.mass)
	physobj:SetVelocity(self.Entity.velocity)
	physobj:SetDamping(0, 0)
	physobj:EnableDrag(false)
	self.Entity.Alpha = 255
	self.Entity.fading = false
	self.Entity:SetColor(50, 50, 50, 255)
	self.Buoyancy = 0.01
	self.Entity.deletetime = CurTime() + GetConVarNumber("sv_CDamage_wreckage_timeout")
	
	self.Entity.CDamage = self.Entity.CDamage or {}
	self.Entity.CDamage.type = "wreckage"
end

function ENT:Think()
	if (GetConVarNumber("sv_CDamage_wreckage_timeout") != -1) and ((self.Entity.deletetime or 0) <= CurTime()) then
		self.Entity.fading = true
	end
	if self.Entity.fading then
		self.Entity.Alpha = math.Clamp(self.Entity.Alpha - 10, 0, 255)
		self.Entity:SetColor(50, 50, 50, self.Entity.Alpha)
		if (self.Entity.Alpha or 0) == 0 then
			self.Entity:Remove()
		end
	end
	self.Entity:GetPhysicsObject():SetBuoyancyRatio(self.Buoyancy)
	self.Buoyancy = math.Clamp((self.Buoyancy - 0.0001), 0, 0.01)
	if (self.Entity:WaterLevel() > 0 and self.Entity:IsOnFire()) then self.Entity:Extinguish() end
	self.Entity:NextThink(CurTime() + 0.05)
end